#pragma once
#include<iostream>
#include<string>
#include<functional>
#include<pthread.h>
#include "log.hpp"
using namespace std;
using fuc_t =function<void(const string& name)>; 
class thread
{
	static void* threadroutine(void* args)//使用静态函数来让参数少一个this指针
	{
		thread* self = static_cast<thread*> (args);
		self->_status = true;
	//	LOG(DEBUG,"%s要执行函数了\n",self->_name.c_str());
		self->_fuc(self->_name);
		self->_status = false;
		return nullptr;
	}
public:
	thread(fuc_t fuc,const string& name)
		:_fuc(fuc),_name(name)
	{
	};
	void start()
	{
		int n =pthread_create(&_tid,nullptr,threadroutine,this);
		if(n!=0) cout<<"error:create thread err!"<<endl;
	};
	void join()
	{
		pthread_join(_tid,nullptr);
	};
	void stop()//终止正在运行的线程
	{
		if(_status)
		{
			_status =false;
			pthread_cancel(_tid);
		};
	};
	~thread(){
		//printf("1\n");
	}

	bool getstatus()
	{return _status;};
	const string& getname()
	{return _name;};
private:
	pthread_t _tid;
        string _name;
	fuc_t _fuc;
 	volatile bool _status;//这个状态表示是否开启	
};
